﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Synergy
{
    public partial class FormClly : Form
    {
        public FormClly()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
                //string fileName = "";
                //string saveFileName = "";
                //SaveFileDialog saveDialog = new SaveFileDialog();
                //saveDialog.DefaultExt = "xls";
                //saveDialog.Filter = "Excel文件|*.xls";
                //saveDialog.FileName = fileName;
                //saveDialog.ShowDialog();
                //saveFileName = saveDialog.FileName;
                //if (saveFileName.IndexOf(":") < 0) return; //被点了取消
                //Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                //if (xlApp == null)
                //{
                //    MessageBox.Show("无法创建Excel对象，您的电脑可能未安装Excel");
                //    return;
                //}
                //Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                //Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                //Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
                ////写入标题             
                //for (int i = 0; i < dataGridView1.ColumnCount; i++)
                //{ worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; }
                ////写入数值
                //for (int r = 0; r < dataGridView1.Rows.Count; r++)
                //{
                //    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                //    {
                //        worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;
                //    }
                //    System.Windows.Forms.Application.DoEvents();
                //}
                //worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
                //MessageBox.Show(fileName + "资料保存成功", "提示", MessageBoxButtons.OK);
                //if (saveFileName != "")
                //{
                //    try
                //    {
                //        workbook.Saved = true;
                //        workbook.SaveCopyAs(saveFileName);  //fileSaved = true;                 
                //    }
                //    catch (Exception ex)
                //    {//fileSaved = false;                      
                //        MessageBox.Show("导出文件时出错,文件可能正被打开！\n" + ex.Message);
                //    }
                //}
                //xlApp.Quit();
                //GC.Collect();//强行销毁           }
                           
           
            }

        private void button刷新_Click(object sender, EventArgs e)
        {
          
                List材料();
           
        }

        private void List材料()
        {

            string sqlidhid = " select 生产订单号,生产订单行号 from [Z_baogong] where id='"+ this.textBox1.Text +"'";
            DataTable tbidhid = pub.U8GetTable(sqlidhid);

            string 生产订单号 = tbidhid.Rows[0]["生产订单号"].ToString();
            string 生产订单行号 = tbidhid.Rows[0]["生产订单行号"].ToString();

            string ccddid = Getccddid(生产订单号);
            string ccddzid = Getccddzid(ccddid, 生产订单行号);

            textBox生产订单行号.Text=生产订单行号;
            textBox生产订单号.Text = 生产订单号;

            string sql;
            ///最大顺序号的报工单，未入库的
            sql = @" select '' 行号,inventory.cinvcode 物料编号,inventory.cinvname 物料名称,qty 应领数量,issqty 已领数量 from v_mom_moallocate_rpt join inventory on inventory.cinvcode=v_mom_moallocate_rpt.invcode where modid='" + ccddzid + "'   ";

            sql += "  order by  modid ";
            DataTable tb = pub.U8GetTable(sql);
            this.dataGridView1.AutoGenerateColumns = false;

            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            if (tb != null)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    tb.Rows[i]["行号"] = (i + 1).ToString();
                }
            }

            this.dataGridView1.DataSource = tb;
            this.dataGridView1.Refresh();
        }

        private string Getccddzid(string ccddid, string 生产订单行号)
        {
            string sql;
            sql = " select modid from v_mom_orderdetail_rpt where moid='" + ccddid + "' and sortSeq='" + 生产订单行号 + "' ";
            DataTable tb = pub.U8GetTable(sql);
            if (tb.Rows.Count > 0)
            {
                return tb.Rows[0][0].ToString();
            }
            else
            {
                return "0";
            }
        }

        private string Getccddid(string 生产订单号)
        {         
            string sql;
            sql = " select moid from v_mom_order_rpt where mocode='" + 生产订单号 + "' ";
            DataTable tb = pub.U8GetTable(sql);
            if (tb.Rows.Count > 0)
            {
                return tb.Rows[0][0].ToString();
            }
            else
            {
                return "0";
            }
        }
        
        
    }
}
